package cn.iocoder.yudao.module.ad.controller.admin.legaladviceclue;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ad.controller.admin.legaladviceclue.vo.*;
import cn.iocoder.yudao.module.ad.dal.dataobject.legaladviceclue.AdLegalAdviceClueDO;
import cn.iocoder.yudao.module.ad.service.legaladviceclue.AdLegalAdviceClueService;

@Tag(name = "管理后台 - 法律咨询线索")
@RestController
@RequestMapping("/ad/legal-advice-clue")
@Validated
public class AdLegalAdviceClueController {

    @Resource
    private AdLegalAdviceClueService legalAdviceClueService;

    @PostMapping("/create")
    @Operation(summary = "创建法律咨询线索")
    @PreAuthorize("@ss.hasPermission('ad:legal-advice-clue:create')")
    public CommonResult<Long> createLegalAdviceClue(@Valid @RequestBody AdLegalAdviceClueSaveReqVO createReqVO) {
        return success(legalAdviceClueService.createLegalAdviceClue(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新法律咨询线索")
    @PreAuthorize("@ss.hasPermission('ad:legal-advice-clue:update')")
    public CommonResult<Boolean> updateLegalAdviceClue(@Valid @RequestBody AdLegalAdviceClueSaveReqVO updateReqVO) {
        legalAdviceClueService.updateLegalAdviceClue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除法律咨询线索")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ad:legal-advice-clue:delete')")
    public CommonResult<Boolean> deleteLegalAdviceClue(@RequestParam("id") Long id) {
        legalAdviceClueService.deleteLegalAdviceClue(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得法律咨询线索")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ad:legal-advice-clue:query')")
    public CommonResult<AdLegalAdviceClueRespVO> getLegalAdviceClue(@RequestParam("id") Long id) {
        AdLegalAdviceClueDO legalAdviceClue = legalAdviceClueService.getLegalAdviceClue(id);
        return success(BeanUtils.toBean(legalAdviceClue, AdLegalAdviceClueRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得法律咨询线索分页")
    @PreAuthorize("@ss.hasPermission('ad:legal-advice-clue:query')")
    public CommonResult<PageResult<AdLegalAdviceClueRespVO>> getLegalAdviceCluePage(@Valid AdLegalAdviceCluePageReqVO pageReqVO) {
        PageResult<AdLegalAdviceClueDO> pageResult = legalAdviceClueService.getLegalAdviceCluePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AdLegalAdviceClueRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出法律咨询线索 Excel")
    @PreAuthorize("@ss.hasPermission('ad:legal-advice-clue:export')")
    @OperateLog(type = EXPORT)
    public void exportLegalAdviceClueExcel(@Valid AdLegalAdviceCluePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdLegalAdviceClueDO> list = legalAdviceClueService.getLegalAdviceCluePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "法律咨询线索.xls", "数据", AdLegalAdviceClueRespVO.class,
                        BeanUtils.toBean(list, AdLegalAdviceClueRespVO.class));
    }

}